using System;
using System.Collections.Generic;
using Gtk;
using Sqlpager;

public partial class TicketForm : Gtk.Dialog
{
	Ticket mTicket;
	ComboDate cboDate;
	
	public Ticket Ticket {get{return mTicket;}}
	
	public TicketForm(Window main, Ticket ticket)
	{
		this.Build();
		this.TransientFor = main;
		this.WindowPosition = WindowPosition.CenterOnParent;
		
		cboDate = new ComboDate();
		hboxDate.Add(cboDate);
		hboxDate.ShowAll();
		//CenterWindow();
		mTicket = ticket;
		FillComboStatus();
		if(mTicket!=null){ 	// updating
			FillForm();
		} else {			// adding
			mTicket = new Ticket();
			Title = "New ticket";
			txtNumber.Text = "Autonumber";
			cboDate.Date = DateTime.Today;
		}
		
	}
	
	void FillComboStatus()
	{
		cboStatus.Clear();
		CellRendererText cell = new CellRendererText();
		cboStatus.PackStart(cell, false);
        cboStatus.AddAttribute(cell, "text", 0);
        ListStore store = new ListStore(typeof (string));
        cboStatus.Model = store;
		List<string> values = new List<string>();
		values.Add("open");
        values.Add("waiting");
        values.Add("working");
        values.Add("closed");
		foreach(string s in values)
			store.AppendValues(s);
		
		if(mTicket!=null){
			int item = 0;
			if(values.Contains(mTicket.Status))
				item = values.IndexOf(mTicket.Status);
        	Gtk.TreeIter iter;
        	cboStatus.Model.IterNthChild (out iter, item);
        	cboStatus.SetActiveIter (iter);
		}
	}

	void FillForm()
	{
		txtNumber.Text = mTicket.Number.ToString();
		//txtNumber.IsEditable = false;
		cboDate.Date = mTicket.Date;
		txtCustomer.Text = mTicket.Customer;
		cboDate.Date = mTicket.Date;
		txtProblem.Buffer.Text = mTicket.Problem;
		txtIp.Text = mTicket.Ip;
		chkActive.Active = mTicket.Active;
		this.Title = "Editing Ticket Nr: "+ mTicket.Number;
	}
	
	void OnSaveClick (object sender, System.EventArgs e)
	{
		// validate backward for grabfocus
		List<string> errors = new List<string>();
		
		TreeIter iter;
		if (!cboStatus.GetActiveIter (out iter)){
			errors.Insert(0,"Select Status.");
			//Console.WriteLine ((string) cboStatus.Model.GetValue (iter, 0));
			cboStatus.GrabFocus();
		}		
		if(!Util.IsIpAddress(txtIp.Text)) {
			errors.Insert(0,"Wrong IP Address.");
			txtIp.GrabFocus();
		}
		if(String.IsNullOrEmpty(txtProblem.Buffer.Text)) {
			errors.Insert(0,"Problem cannot be empry.");
			txtProblem.GrabFocus();
		}
		if(String.IsNullOrEmpty(txtCustomer.Text)) {
			errors.Insert(0,"Customer cannot be empty.");
			txtCustomer.GrabFocus();
		}
		if(!Util.IsDate(cboDate.Text)) {
			errors.Insert(0,"Wrong date.");
			cboDate.GrabFocus();
		}	
		if(errors.Count > 0){
			ShowMessage(errors.ToArray());
			return;
		} else {	// save
			FillTicket();
			this.Respond(Gtk.ResponseType.Ok);
		}
	}		
	
	void FillTicket()
	{
		mTicket.Number = Util.CInt(txtNumber.Text);
		mTicket.Date = Util.CDate(cboDate.Text);
		mTicket.Customer = txtCustomer.Text;
		mTicket.Problem = txtProblem.Buffer.Text;
		mTicket.Ip = txtIp.Text;
		
		TreeIter iter;
		cboStatus.GetActiveIter (out iter);
		mTicket.Status = (string) cboStatus.Model.GetValue(iter, 0);
		
	}
	void ShowMessage(string[] errors)
	{
		string messge = 
			"Data validation failed.\n"+
			"Your input seems to be incorrect:\n\n";
		for(int i=0; i<errors.Length; i++)
			messge += (i+1) + ": " + errors[i] + "\n";
		MessageDialog m = new MessageDialog(
			this, DialogFlags.DestroyWithParent,
        	MessageType.Info, ButtonsType.Ok, messge);
		m.Run ();
    	m.Destroy();	
	}
}

